﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Grizzy_X
{
    public partial class ImportForm : Form
    {
        public ImportForm()
        {
            InitializeComponent();
        }
        public int width;
        public int height;
        public int x;
        public int y;
        public string basename;
        private void 浏览button_Click(object sender, EventArgs e)
        {
            Persistance.Openfiledialog();
            if (Controller.check == true)
            {
                Image basemap = Image.FromFile(Controller.filename);
                Bitmap basebmp = new Bitmap(preview.Width, preview.Height);
                Graphics a = Graphics.FromImage(basebmp);
                Rectangle destRec = new Rectangle(0, 0, preview.Width, preview.Height);
                Rectangle srcRect = new Rectangle(0, 0, basemap.Width,basemap.Height);
                a.DrawImage(basemap,  destRec, srcRect,GraphicsUnit.Pixel);
                preview.Image = basebmp;
                a.Dispose();
                Controller.check = false;
            }
        }

        private void 确定button_Click(object sender, EventArgs e)
        {
            Controller.CountBase++;
            CBaseMap bm = new CBaseMap();
            bm.New(basenametextBox.Text);
            bm.FilePath = Controller.filename;
            double trans;
            if (unit.SelectedIndex == 1)
                trans = 96;
            else
                trans = 96 / 2.54;
            if (cucheckBox.Checked == true)
            {
                bm.Width = Controller.View_Width;
                bm.Height = Controller.View_Height;
                bm.x = 0;
                bm.y = 0;
            }
            if (cucheckBox.Checked == false)
            {
                bm.Width = (int)(Convert.ToDouble(widthtextbox.Text)*trans);
                bm.Height = (int)(Convert.ToDouble(heighttextbox.Text) * trans);
                //left top
                if (position.SelectedIndex == 1)
                {
                    bm.x = (int)(Convert.ToDouble(xtextbox.Text) * trans);
                    bm.y = (int)(Convert.ToDouble(ytextbox.Text) * trans);
                }
                //right bottom
                else if (position.SelectedIndex == 2)
                {
                    bm.x = (int)(Controller.View_Width - Convert.ToDouble(xtextbox.Text) * trans - bm.Width);
                    bm.y = (int)(Controller.View_Height - Convert.ToDouble(ytextbox.Text) * trans - bm.Height);
                }
                //right top
                else if (position.SelectedIndex == 3)
                {
                    bm.x = (int)(Controller.View_Width - Convert.ToDouble(xtextbox.Text) * trans - bm.Width);
                    bm.y = (int)(Convert.ToDouble(ytextbox.Text) * trans);
                }
                //left bottom
                else
                {
                    bm.x = (int)(Convert.ToDouble(xtextbox.Text)*trans);
                    bm.y = (int)(Controller.View_Height  - Convert.ToDouble(ytextbox.Text) * trans- bm.Height);
                }
            }
            Controller.MyMap.BaseMap_List.Add(bm);
            this.Close();
            Controller.check = true;
        }

        private void ImportForm_Activated(object sender, EventArgs e)
        {
            basenametextBox.Text = "新建底图" + Convert.ToString(Controller.CountBase+1);
            basenametextBox.Focus();
        }

        private void cucheckBox_CheckStateChanged(object sender, EventArgs e)
        {
                Customize.Enabled = !(Customize.Enabled);
        }

        private void unit_SelectedValueChanged(object sender, EventArgs e)
        {
            if (unit.SelectedIndex == 0)
            {
                widthtextbox.Text = Convert.ToString(Math.Round((Convert.ToDouble(widthtextbox.Text) * 2.54),1));
                heighttextbox.Text = Convert.ToString(Math.Round((Convert.ToDouble(heighttextbox.Text) * 2.54),1));
                xtextbox.Text = Convert.ToString(Math.Round((Convert.ToDouble(xtextbox.Text)*2.54),1));
                ytextbox.Text = Convert.ToString(Math.Round((Convert.ToDouble(ytextbox.Text) * 2.54),1));
            }
            else
            {
                widthtextbox.Text = Convert.ToString(Math.Round((Convert.ToDouble(widthtextbox.Text) / 2.54),1));
                heighttextbox.Text = Convert.ToString(Math.Round((Convert.ToDouble(heighttextbox.Text) / 2.54),1));
                xtextbox.Text = Convert.ToString(Math.Round((Convert.ToDouble(xtextbox.Text)/ 2.54),1));
                ytextbox.Text = Convert.ToString(Math.Round((Convert.ToDouble(ytextbox.Text) / 2.54),1));
            }
            widthtextbox.Refresh();
            heighttextbox.Refresh();
            xtextbox.Refresh();
            ytextbox.Refresh();
        }

        private void 取消button_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
